package com.fwzc.giuseppe.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.adapter.ShopAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.config.ShoppingCanst;
import com.fwzc.giuseppe.config.shopBean;
import com.lidroid.xutils.util.LogUtils;

public class ShoppingCartAct extends BaseActivity implements OnClickListener{

	private CheckBox checkBox;
	private ListView listView;
	private TextView popTotalPrice;		//结算的价格
//	private TextView popDelete;			//删除
//	private TextView popRecycle;		//收藏
	private Button popCheckOut;		//结算
	private ShopAdapter adapter;		//自定义适配器
	private List<shopBean> list;		//购物车数据集合
	float price = 0;
	private boolean flag = true;		//全选或全取消
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopping_cart);
		initBar();
		actionbar_side_name.setText("购物车");
		bg_actionbar_side.setBackgroundColor(getResources().getColor(R.color.transparent));
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);
		
		tv_actionbar_side_right.setText("删除");
		tv_actionbar_side_right.setOnClickListener(this);
		initViews();
		init();
	}
	
	//初始化UI界面
	private void initViews(){
		checkBox = (CheckBox) findViewById(R.id.all_check);
		listView = (ListView) findViewById(R.id.shop_listview);
		popTotalPrice = (TextView) findViewById(R.id.shopTotalPrice);

		popCheckOut = (Button) findViewById(R.id.checkOut);
		

		checkBox.setOnClickListener(this);
		popCheckOut.setOnClickListener(this);

	}
	
	//初始化数据
	private void init(){
		getListData();
		list = ShoppingCanst.list;
		adapter = new ShopAdapter(this,list,handler,R.layout.item_list_shopping_cart);
		listView.setAdapter(adapter);
	}
	
	//获取集合数据
	private void getListData(){
		ShoppingCanst.list = new ArrayList<shopBean>();
		shopBean bean = new shopBean();
		bean.setShopId(1);
		bean.setShopPicture(R.drawable.shoes1);
		bean.setStoreName("花花公子");
		bean.setShopName("Simier 斯米尔英伦风日常休闲男鞋单鞋 秋季真皮皮鞋鞋子男6639");
		bean.setShopDescription("颜色：蓝色，尺码：41");
		bean.setShopPrice(199);
		bean.setShopNumber(1);
		bean.setChoosed(false);
		ShoppingCanst.list.add(bean);
		shopBean bean2 = new shopBean();
		bean2.setShopId(2);
		bean2.setShopPicture(R.drawable.shoes2);
		bean2.setStoreName("木林森");
		bean2.setShopName("Camel 骆驼男鞋 男士日常休闲皮鞋 2014秋冬真皮系带休闲鞋子男");
		bean2.setShopDescription("颜色：蓝色，尺码：41");
		bean2.setShopPrice(399);
		bean2.setShopNumber(2);
		bean2.setChoosed(false);
		ShoppingCanst.list.add(bean2);
		shopBean bean3 = new shopBean();
		bean3.setShopId(3);
		bean3.setShopPicture(R.drawable.shoes3);
		bean3.setStoreName("西瑞");
		bean3.setShopName("雷艾新款男鞋子 韩版 潮流 男鞋男休闲鞋 板鞋 鞋子 男 休闲皮鞋");
		bean3.setShopDescription("颜色：黑色，尺码：41");
		bean3.setShopPrice(198);
		bean3.setShopNumber(5);
		bean3.setChoosed(false);
		ShoppingCanst.list.add(bean3);
		shopBean bean4 = new shopBean();
		bean4.setShopId(4);
		bean4.setShopPicture(R.drawable.shoes4);
		bean4.setStoreName("古奇天伦");
		bean4.setShopName("奥康男鞋春秋透气系带板鞋男韩版潮流男士休闲鞋真皮低帮鞋子男");
		bean4.setShopDescription("颜色：蓝色，尺码：41");
		bean4.setShopPrice(599);
		bean4.setShopNumber(1);
		bean4.setChoosed(false);
		ShoppingCanst.list.add(bean4);
	}

	
	//结算
	private void goCheckOut(){
		String shopIndex = deleteOrCheckOutShop();
		Intent checkOutIntent = new Intent(ShoppingCartAct.this,PayActivity.class);
		checkOutIntent.putExtra("shopIndex", shopIndex);
		checkOutIntent.putExtra("TotalPrice", price);
		startActivity(checkOutIntent);
	}
	
	//全选或全取消
	private void selectedAll(){
		for(int i=0;i<list.size();i++){
			ShopAdapter.getIsSelected().put(i, flag);
		}
		adapter.notifyDataSetChanged();
	}
	
	//删除或结算商品
	private String deleteOrCheckOutShop(){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<list.size();i++){
			if(ShopAdapter.getIsSelected().get(i)){
				sb.append(i);
				sb.append(",");
			}
		}
		//sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
	
	//弹出对话框询问用户是否删除被选中的商品
	private void showDialogDelete(String str){
		final String[] delShopIndex = str.split(",");
		new AlertDialog.Builder(ShoppingCartAct.this)
		.setMessage("您确定删除这"+delShopIndex.length+"商品吗？")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				for(String s : delShopIndex){
					int index = Integer.valueOf(s);
					list.remove(index);
					ShoppingCanst.list.remove(index);
					//连接服务器之后，获取数据库中商品对应的ID，删除商品
					list.get(index).getShopId();
				}
				flag = false;
				selectedAll();	//删除商品后，取消所有的选择
				flag = true;	//刷新页面后，设置Flag为true，恢复全选功能
			}
		}).setNegativeButton("取消", null)
		.create().show();
	}
	
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == 10){	//更改选中商品的总价格
				price = (Float)msg.obj;
				LogUtils.d("--------------------"+price);
				if(!"".equals(price)&&price>0){
					popTotalPrice.setText("￥"+price);
					popTotalPrice.setVisibility(View.VISIBLE);
					popCheckOut.setEnabled(true);
					tv_actionbar_side_right.setVisibility(View.VISIBLE);
				}else{
					popTotalPrice.setVisibility(View.GONE);
					popCheckOut.setEnabled(false);
					tv_actionbar_side_right.setVisibility(View.GONE);
				}
			}else if(msg.what == 11){
				//所有列表中的商品全部被选中，让全选按钮也被选中
				//flag记录是否全被选中
				flag = !(Boolean)msg.obj;
				checkBox.setChecked((Boolean)msg.obj);
			}
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		// 返回
		case R.id.iv_actionbar_side_left:
			finish();
			break;
		//全选
		case R.id.all_check:		
			selectedAll();
			break;
		//删除
		case R.id.tv_actionbar_side_right:			
			
			String shopIndex = deleteOrCheckOutShop();
			showDialogDelete(shopIndex);
			break;
		//结算
		case R.id.checkOut:			
			goCheckOut();
			break;
			
			default:
				break;
		}
	}
}
